package com.example.gw_back_end.controller;

import com.example.gw_back_end.domain.PageParam;
import com.example.gw_back_end.domain.Result;
import com.example.gw_back_end.domain.entities.Order;
import com.example.gw_back_end.service.OrderService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
public class OrderController {

    private final OrderService orderService;
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    // 新增订单
    @PostMapping
    public Result addOrder(Integer gameId) {
        if (orderService.addOrder(gameId)) {
            return Result.success();
        } else {
            return Result.error();
        }
    }

    @PostMapping("/cart")
    public Result addOrderByCart() {
        if (orderService.addOrderByCart()) {
            return Result.success();
        } else {
            return Result.error();
        }
    }

    // 删除订单
    @DeleteMapping
    public Result deleteOrder(Integer orderId) {
        if(orderService.deleteOrder(orderId)){
            return Result.success();
        } else {
            return Result.error();
        }
    }

    // 更新订单
    @PutMapping
    public Result updateOrder(Order order) {
        if (orderService.updateOrder(order)) {
            return Result.success();
        } else {
            return Result.error();
        }
    }

    // 获取订单
    @GetMapping
    public Result getOrder(Integer orderId) {
        Order order = orderService.getOrderById(orderId);
        if (order != null) {
            return Result.success(order);
        } else {
            return Result.error("订单获取失败！");
        }
    }

    // 分页获取订单
    @GetMapping("list")
    public Result getOrderList(PageParam pageParam,@RequestParam(defaultValue = "-1") Integer status,@RequestHeader("X-Client-Type") String clientType) {
        return Result.success(orderService.getAllOrders(pageParam,clientType));
    }
}
